﻿using Infrastructure.Events;
using Infrastructure.ViewModels;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;

namespace DocumentDetailsModule.Views
{
    /// <summary>
    /// Document details presenter class
    /// </summary>
    public class DocumentDetailsPresenter : IDocumentDetailsPresenter
    {
        IUnityContainer _container;
        readonly IEventAggregator _eventAggregator;
        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>The view.</value>
        public IDocumentDetailsView View { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentDetailsPresenter"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="view">The view.</param>
        public DocumentDetailsPresenter(IUnityContainer container, IEventAggregator eventAggregator, IDocumentDetailsView view)
        {
            _container = container;
            _eventAggregator = eventAggregator;
            View = view;

            // Subscribe for document selected event
            DocumentSelectedEvent documentSelectedEvent = _eventAggregator.GetEvent<DocumentSelectedEvent>();
            documentSelectedEvent.Subscribe(DocumentSelectedEventHandler, true);
        }

        /// <summary>
        /// New document selected in the application
        /// </summary>
        /// <param name="catalogItemVM">The selected document</param>
        void DocumentSelectedEventHandler(CatalogItemViewModel catalogItemVM)
        {
            View.Model = catalogItemVM;
        }
    }
}
